package com.yonyou.pmclouds.organization.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.organization.entity.OrgAndRoleVO;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrgAndRoleQuery;
import com.yonyou.pmclouds.organization.rmiitf.OrgOutterMaintain;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationMaintain;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= OrgOutterMaintain.class)
public class OrgOutterMaintainImpl implements OrgOutterMaintain {

    @Autowired
    private OrganizationQuery organizationQuery;
    @Autowired
    private OrganizationMaintain organizationMaintain;
    @Autowired
    private OrgAndRoleQuery orgAndRoleQuery;

    @Override
    public OrganizationVO insertOrgFromOutter(OrganizationVO organizationVO) throws BusinessException {

        OrganizationVO oldOrg = getOrigOrgVO(organizationVO);
        if(oldOrg != null)
            throw new BusinessException("不可重复插入");

        return organizationMaintain.insertOrgs(new OrganizationVO[]{organizationVO})[0];
    }

    @Override
    public OrganizationVO updateOrgFromOutter(OrganizationVO organizationVO) throws BusinessException {

        OrganizationVO oldOrg = getOrigOrgVO(organizationVO);

        if(oldOrg == null){
            return organizationMaintain.insertOrgs(new OrganizationVO[]{organizationVO})[0];
        }else{
            organizationVO.setPkOrganization(oldOrg.getPkOrganization());
            organizationVO.setChangeFlag(oldOrg.getChangeFlag());

            return organizationMaintain.updateOrg(new OrganizationVO[]{organizationVO},
                    new String[]{OrganizationVO.SNAME,OrganizationVO.SCODE,OrganizationVO.PK_PARENT,
                            OrganizationVO.END_DATE,OrganizationVO.START_DATE,OrganizationVO.PK_FINANCE_ORG})[0];
        }
    }

    @Override
    public OrganizationVO deleteOrgFromOutter(OrganizationVO organizationVO) throws BusinessException {

        OrganizationVO oldOrg = getOrigOrgVO(organizationVO);

        OrgAndRoleVO[] orgAndRoleVOS =  orgAndRoleQuery.queryListByOrg(RuntimeEnvironment.getTenantId(),oldOrg.getPkOrganization());

        if(ArrayUtils.isNotEmpty(orgAndRoleVOS))
            throw new BusinessException("组织已被分配");

        if(oldOrg != null){
            return organizationMaintain.deleteOrg(oldOrg);
        }

        return null;
    }

    @Override
    public OrganizationVO update4Enable(OrganizationVO organizationVO) throws BusinessException {

        OrganizationVO oldOrg = getOrigOrgVO(organizationVO);

        if(oldOrg != null)
            organizationMaintain.update4Enable(new OrganizationVO[]{oldOrg});
        return null;
    }

    @Override
    public OrganizationVO update4Disable(OrganizationVO organizationVO,Set<String> roleSet) throws BusinessException {

        OrganizationVO oldOrg = getOrigOrgVO(organizationVO);

        OrgAndRoleVO[] orgAndRoleVOS =  orgAndRoleQuery.queryListByOrg(RuntimeEnvironment.getTenantId(),oldOrg.getPkOrganization());

        if(ArrayUtils.isNotEmpty(orgAndRoleVOS))
            throw new BusinessException("组织已被分配");

        if(oldOrg != null)
            return organizationMaintain.update4Disable(new OrganizationVO[]{oldOrg},roleSet)[0];
        return null;
    }

    private OrganizationVO getOrigOrgVO(OrganizationVO organizationVO) throws BusinessException{

        String pkSrc = organizationVO.getPkSrc();

        if(StringUtils.isEmpty(pkSrc))
            throw new BusinessException("来源主键为空");

        OrganizationVO oldOrg = organizationQuery.queryByPkSrc(RuntimeEnvironment.getTenantId(),pkSrc);

        return oldOrg;

    }
}
